package net.lab1024.sa.admin.module.app.service;

import cn.hutool.core.collection.CollUtil;
import com.baomidou.mybatisplus.extension.toolkit.Db;
import lombok.extern.slf4j.Slf4j;
import net.lab1024.sa.admin.module.app.req.AppDollAlbumReq;
import net.lab1024.sa.admin.module.app.req.AppDollBindReq;
import net.lab1024.sa.admin.module.app.req.AppDollDetailReq;
import net.lab1024.sa.admin.module.app.rsp.AppDollDetailRsp;
import net.lab1024.sa.admin.module.business.bjd.domain.entity.DollEntity;
import net.lab1024.sa.admin.module.business.bjd.domain.entity.DollPhotosEntity;
import net.lab1024.sa.admin.module.business.bjd.domain.entity.OrderEntity;
import net.lab1024.sa.admin.module.business.bjd.domain.entity.UserDollBindEntity;
import net.lab1024.sa.admin.util.AdminRequestUtil;
import net.lab1024.sa.base.common.domain.RequestUser;
import net.lab1024.sa.base.common.exception.BusinessException;
import net.lab1024.sa.base.common.util.SmartRequestUtil;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

/**
 * 类的描述
 *
 * @author guolong.zhang
 * @date 2025/02/10 09:28
 **/
@Service
@Slf4j
public class AppDollService {
    public void bind(AppDollBindReq req) {
        RequestUser requestUser = SmartRequestUtil.getRequestUser();
        String orderNo = req.getOrderNo();
        UserDollBindEntity bindEntity = Db.lambdaQuery(UserDollBindEntity.class)
                .eq(UserDollBindEntity::getOrderNo, orderNo)
                .last(" limit 1")
                .one();
        if(bindEntity !=null){
            throw new BusinessException("已经存在订单编号的绑定关系");
        }
        bindEntity = new UserDollBindEntity();
        bindEntity.setOrderNo(orderNo);
        bindEntity.setUserId(requestUser.getUserId());
        Db.save(bindEntity);
    }

    public void nickName(AppDollDetailReq req) {
        String orderNo = req.getOrderNo();
        OrderEntity orderEntity = Db.lambdaQuery(OrderEntity.class)
               .eq(OrderEntity::getOrderNo, orderNo)
               .last(" limit 1")
               .one();
        if(orderEntity == null){
            throw new BusinessException("订单信息不存在");
        }
        Db.lambdaUpdate(OrderEntity.class)
                .set(OrderEntity::getNickName,req.getUserNickName())
                .eq(OrderEntity::getOrderNo,orderNo)
                .update();

    }

    public AppDollDetailRsp detail(AppDollDetailReq req) {
        RequestUser requestUser = SmartRequestUtil.getRequestUser();
        AppDollDetailRsp rsp = new AppDollDetailRsp();
        UserDollBindEntity bindEntity = Db.lambdaQuery(UserDollBindEntity.class)
                .eq(UserDollBindEntity::getOrderNo, req.getOrderNo())
                .eq(UserDollBindEntity::getUserId, requestUser.getUserId())
                .last(" limit 1")
                .one();
        if(bindEntity != null){
            rsp.setBindStatus(1);
        }
        OrderEntity orderEntity = Db.lambdaQuery(OrderEntity.class)
                .eq(OrderEntity::getOrderNo, req.getOrderNo())
                .last(" limit 1")
                .one();
        if(orderEntity == null){
            throw new BusinessException("订单信息不存在");
        }
        DollEntity doll = Db.lambdaQuery(DollEntity.class)
                .eq(DollEntity::getDollNo, orderEntity.getDollNo())
                .one();
        if(doll == null){
            throw new BusinessException("娃娃信息不存在");
        }
        rsp.setSize(doll.getSize());
        rsp.setSkin(doll.getSkin());
        rsp.setType(doll.getType());
        rsp.setImgUrl(doll.getImgUrl());
        rsp.setDollNo(doll.getDollNo());
        rsp.setOfficialName(doll.getOfficialName());
        rsp.setUserNickName(orderEntity.getNickName());

        rsp.setDollPhone(orderEntity.getDollPhone());
        rsp.setDollName(orderEntity.getDollName());
        rsp.setOrderNo(orderEntity.getOrderNo());
        rsp.setLogisticsNo(orderEntity.getLogisticsNo());

        List<DollPhotosEntity> photosEntityList = Db.lambdaQuery(DollPhotosEntity.class)
                .eq(DollPhotosEntity::getOrderNo, orderEntity.getOrderNo())
                .list();
        if (CollUtil.isNotEmpty(photosEntityList)) {
            List<AppDollDetailRsp.AlbumImageArray> list = new ArrayList<>();
            for (DollPhotosEntity photosEntity : photosEntityList) {
                AppDollDetailRsp.AlbumImageArray array = new AppDollDetailRsp.AlbumImageArray();
                array.setImageUrl(photosEntity.getImageUrl());
                array.setImageType(photosEntity.getImageType());
                list.add(array);
            }
            rsp.setAlbumImageArray(list);
        }
        return rsp;
    }

    @Transactional(rollbackFor = Exception.class)
    public void album(AppDollAlbumReq req) {

        if(CollUtil.isNotEmpty(req.getAlbumImageArray())){
            Db.lambdaUpdate(DollPhotosEntity.class)
                    .eq(DollPhotosEntity::getOrderNo, req.getOrderNo())
                    .remove();
            for (AppDollAlbumReq.AlbumImageArray array : req.getAlbumImageArray()) {
                DollPhotosEntity photosEntity = new DollPhotosEntity();
                photosEntity.setOrderNo(req.getOrderNo());
                photosEntity.setImageType(array.getImageType());
                photosEntity.setImageUrl(array.getImageUrl());
                Db.save(photosEntity);
            }
        }
    }

    public void unbind(AppDollBindReq req) {
        RequestUser requestUser = SmartRequestUtil.getRequestUser();
        String orderNo = req.getOrderNo();
        Db.lambdaUpdate(UserDollBindEntity.class)
                .eq(UserDollBindEntity::getOrderNo, orderNo)
                .eq(UserDollBindEntity::getUserId, requestUser.getUserId())
                .remove();
    }
}
